web3.js@0.2x.x web3.eth.contract
web3.eth.contract(abiArray)
Solidity コントラクトのためのコントラクトオブジェクトを作成します。
パラメータ
1. Array
コントラクトのイベント及び関数を伴うABI 配列
戻り値
Object - コントラクトオブジェクトです。つぎのように使用することができます。
コントラクトオブジェクトの使用方法
code:web3.eth.contract.js
var MyContract = web3.eth.contract(abiArray);
// instantiate by address
var contractInstance = MyContract.at(address);
// deploy new contract
var contractInstance = MyContract.new(constructorParam1 constructorParam2, {data: '0x12345...', from: myAccount, gas: 1000000}); // Get the data to deploy the contract manually
var contractData = MyContract.new.getData(constructorParam1 constructorParam2, {data: '0x12345...'}); // contractData = '0x12345643213456000000000023434234'
任意のアドレスを指定してすでに存在するコントラクトを呼び出したり、コンパイル済みのバイトコードを使用してコントラクトをデプロイすることができます。
code:web3.eth.contract.js
// Instantiate from an existing address:
var myContractInstance = MyContract.at(myContractAddress);
// Or deploy a new contract:
// Deploy the contract asynchronous from Solidity file:
...
const fs = require("fs");
const solc = require('solc')
let source = fs.readFileSync('nameContract.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let gasEstimate = web3.eth.estimateGas({data: bytecode});
let MyContract = web3.eth.contract(JSON.parse(abi));
var myContractReturned = MyContract.new(param1, param2, {
from:mySenderAddress,
data:bytecode,
gas:gasEstimate}, function(err, myContract){
if(!err) {
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if(!myContract.address) {
console.log(myContract.transactionHash) // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
console.log(myContract.address) // the contract address
}
// Note that the returned "myContractReturned" === "myContract",
// so the returned "myContractReturned" object will also get the address set.
}
});
// Deploy contract syncronous: The address will be added as soon as the contract is mined.
// Additionally you can watch the transaction by using the "transactionHash" property
var myContractInstance = MyContract.new(param1, param2, {data: myContractCode, gas: 300000, from: mySenderAddress});
myContractInstance.transactionHash // The hash of the transaction, which created the contract
myContractInstance.address // undefined at start, but will be auto-filled later
上記の使用方法の部分を日本語でわかりやすく説明したい
サンプルコード
code:example.js
// contract abi
var abi = [{
name: 'myConstantMethod',
type: 'function',
constant: true,
inputs: name: 'a', type: 'string' },
}, {
name: 'myStateChangingMethod',
type: 'function',
constant: false,
inputs: name: 'a', type: 'string' }, { name: 'b', type: 'int' },
outputs: []
}, {
name: 'myEvent',
type: 'event',
}];
// creation of contract object
var MyContract = web3.eth.contract(abi);
// initiate contract for an address
var myContractInstance = MyContract.at('0xc4abd0339eb8d57087278718986382264244252f');
// call constant function
var result = myContractInstance.myConstantMethod('myParam');
console.log(result) // '0x25434534534'
// send a transaction to a function
myContractInstance.myStateChangingMethod('someParam1', 23, {value: 200, gas: 2000});
// short hand style
web3.eth.contract(abi).at(address).myAwesomeMethod(...);
// create filter
var filter = myContractInstance.myEvent({a: 5}, function (error, result) {
if (!error)
console.log(result);
/*
{
address: '0x8718986382264244252fc4abd0339eb8d5708727',
topics: "0x12345678901234567890123456789012", "0x0000000000000000000000000000000000000000000000000000000000000005",
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
...
}
*/
});
参考